<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 吃草的鱼 <783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
use Shop\Api\CategoryApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ProductImageApi;
use Weixin\Api\WxuserApi;
use Admin\Api\UserPictureApi;
use Shop\Api\AddressApi;
use Shop\Api\OrdersApi;
use Shop\Api\GroupApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\OrdersContactInfoApi;
class PayController extends HomeController {
        /*
         * 启用HTML5进行支付
         * */
    /*微信支付HTML5的实现*/
    public function jsapi(){
        $counttype=I('counttype');

        if(!empty(I('type'))) $type=I('type');
        if($type=='tuanyuan'){
            $groupid=I('groupid');
            $map['id']=$groupid;
            $group=apiCall(GroupApi::GET_INFO,array($map));
            if(!$group['status']) $this->error("拼团错误");
            if(empty($group['info'])) $this->error("拼团错误");
            if($group['info']['group_sum']<=$group['info']['group_count'])  $this->error("拼团已满",'http://muyin.8raw.com/index.php/Home/Index/index');
        }elseif($type=='tuanzhang'){
            $pid=I('pid');
            $ma=[];$ma['productid']=$pid;$ma['type']=$counttype;
            $vi=apiCall(GroupApi::GET_INFO,array($ma));
            if(!empty($vi['info'])) {

                $order['order_code']    = I('order_code');
                $entity                 = [];
                $entity['status'] = -1;
                $del                    = apiCall(OrdersApi::DELETE, array($order));
            }

            if($del['status']) $this->error('已有团长，请重新拼团吧','http://muyin.8raw.com/index.php/Home/Index/index');

        }

        $map=[];
        $map['order_code'] = I('order_code');

        //查找订单详情
        $orderres = apiCall(OrdersItemApi::GET_INFO,array($map));
        $num=$orderres['info']['count'];
        $money=$orderres['info']['price']*$num;

        $postprice=$orderres['info']['post_price'];
        if($orderres['status']){
            $xctime = time()-$orderres['info']['createtime'];
            if($xctime < 3600){
                $body = '母婴帮转平台-购买'.$orderres['info']['name'];
                $fee = bcadd($money,$postprice,4)*100;
                $config = C("WXPAY_PAY_CONFIG");
                //①、获取用户openid
                $tools = new \Common\Api\Wxpay\JsApi($config);
                $openId =$this->userinfo['openid'];//获得用户的OPenid
                //②、统一下单
                $input = new \Common\Api\Wxpay\WxPayUnifiedOrder();
                $input->setConfig($config);
                $input->SetBody($body);
                $input->SetAttach($counttype);
                $input->SetOut_trade_no($map['order_code']);//随机生成订单号
                $input->SetTotal_fee($fee);
                $input->SetTime_start(date("YmdHis"));
                $input->SetTime_expire(date("YmdHis", time() + 36000));

                if($type == 'tuanzhang'){
                    $input->SetNotify_url("http://muyin.8raw.com/index.php/Home/PAYTest/ajaxinformzhang");
                }elseif($type == 'tuanyuan'){
                    $input->SetNotify_url("http://muyin.8raw.com/index.php/Home/PAYTest/ajaxinformyuan");
                }else {
                    $input->SetNotify_url("http://muyin.8raw.com/index.php/Home/Ajaxinform/ajaxinform");
                }
                $input->SetTrade_type("JSAPI");
                $input->SetOpenid($openId);

                \Common\Api\Wxpay\WxPayApi::setConfig($config);
                $order = \Common\Api\Wxpay\WxPayApi::unifiedOrder($input);


                $jsApiParameters = $tools->GetJsApiParameters($order);

                $this->assign("jsApiParameters",$jsApiParameters);




                $this->display();
            }else{
                $this->error('对不起订单已经失效了','http://muyin.8raw.com/index.php/Home/Index/index');
            }

        }else{
            $this->error('不好意思 支付页跳转失败');
        }

    }


}
